import { Body, Controller, Delete, ParseArrayPipe, Post, Query, Req } from "@nestjs/common";
import { ApiBearerAuth, ApiOperation, ApiTags } from "@nestjs/swagger";
import { Request } from "express";

import AjaxResult from "@/core/utils/AjaxResult";

import { DeleteBatchByUrlDto } from "../dto/delete-batch-by-url.dto";
import { FileService } from "../service/file.service";

@ApiTags("前台文件管理")
@ApiBearerAuth()
@Controller("/front/file")
export class FileController {
    constructor(private readonly fileService: FileService) {}

    @ApiOperation({ summary: "文件批量删除" })
    @Delete("/batch/delete")
    async removeBatch(@Query("ids", new ParseArrayPipe({ separator: "," })) ids: string[], @Req() req: Request) {
        return AjaxResult.success(
            await this.fileService.removeBatch(
                ids.map(id => +id),
                req.user.id
            )
        );
    }

    @ApiOperation({
        summary: "文件批量删除by url",
        description: "富文本编辑器撤销上传的图片后，需要将图片url传给后台，后台删除图片"
    })
    @Post("/batch/deleteByUrl")
    // async removeBatchByUrl(@Body("urls") urls: string[]) { // 不好生成Swagger文档
    async removeBatchByUrl(@Body() dto: DeleteBatchByUrlDto, @Req() req: Request) {
        return AjaxResult.success(await this.fileService.removeBatchByUrl(dto.urls, req.user.id));
    }
}
